<?php
include 'AdminController.php';
class Admin_PaymentHistoryController extends Admin_AdminController
{
	public function init(){
		//set layout
        $layoutPath = APPLICATION_PATH . '/templates/admin/admin';
        $option = array('layout' => 'index', 'layoutPath' => $layoutPath);
        Zend_Layout::startMvc($option);
        
        //アクティブメニューを設定
        $this->view->active_menu = "stats";
	}
	
    public function indexAction ()
    {
    	//TODO 必要なクラスを読み込む
    	Zend_Loader::loadClass("PaymentHistoryModel");
    	Zend_Loader::loadClass("TableModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	//TODO パラメーター取る
    	$start_month = $request->getParam("start_month");
    	$end_month = $request->getParam("end_month");
    	$search_key = $request->getParam('search_key');
    	$page = $request->getParam('page', 1);
    	
    	//TODO　でーたを読み出す 
    	$model = new PaymentHistoryModel();
    	$data = $model->getListRecord($search_key, $start_month, $end_month, NULL, $page, 10);
    	$data_count = $model->getListRecordCount($search_key, $start_month, $end_month, NULL);
    	
    	//TODO データを一覧に表現するためにテーブルモデルを作成
    	$table_model = new TableModel();
    	$table_model->setTable_title("実績と");
    	
    	//TODO テーブルのヘッダーを設定
    	$table_model->addHeader('month', '年月', true);
    	$table_model->addHeader('username', 'ユーザ名', false);
    	$table_model->addHeader('name', 'ユーザの氏名', true);
    	$table_model->addHeader('order_count', '予約数（回）', false);
    	$table_model->addHeader('total_money', '実績（VND）', false);
    	$table_model->addHeader('total_fee', '課徴金（VND）', false);

    	//TODO テーブルのデータを設定
    	//format data
    	foreach ($data as &$row){
    		$row['total_money'] = number_format($row['total_money']);
    		$row['total_fee'] = number_format($row['total_fee']);
    	}
    	$table_model->setTable_data($data);
    	$table_model->setData_count($data_count);
    	$table_model->setPage($page);
    	$table_model->setLine_per_page(10);

    	//TODO テーブルのリンクを設定
    	$table_model->setSearch_link("/admin/payment-history");
    	$table_model->setEdit_link("/admin/payment-history/edit");
    	
    	//TODO　ビューに送る
    	$view->table = $table_model;
    	
    	//TODO　AJAXで検索場合
    	if($request->getParam('search')){
    		//disable layout
    		$layout = $this->_helper->layout();
		    $layout->disableLayout();
		    
		    $view->search_mode = 1;
    	}
    	
    	//TODO notify
    	$view->msg = $request->getParam('msg');
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/user";
    	$nav['実績と課徴参照'] = "/user/payment-history";
    	$view->nav = $nav;
    }
    
	public function editAction(){
    	//TODO　必要なクラスを読み込む
    	Zend_Loader::loadClass("PaymentHistoryModel");
    	Zend_Loader::loadClass("SelectListModel");
    	Zend_Loader::loadClass("UserModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	$edit_id = $request->getParam("edit_id");
    	
    	$model = new PaymentHistoryModel();
    	
    	if(!$request->isPost()){
    		$editObject = $model->fetchRecord($edit_id);
    		$userModel = new UserModel();
    		$user = $userModel->fetchUser($editObject->user_id);
    		
    		$view->edit_id = $editObject->id;
    		$view->month = $editObject->month;
    		
    		$view->user_id = $editObject->user_id;
    		$view->username = $user->username;
    		$view->name = $user->name;
    		
    		$view->total_money = $editObject->total_money;
    		$view->total_fee = $editObject->total_fee;
    		$view->order_count = $editObject->order_count;

    	}else{
    		//TODO　会議室情報を更新
			$data = array(
				'total_money' => $request->getParam("total_money"),
				'total_fee' => $request->getParam("total_fee")
			);    		
    		
			$check = $model->updateData($edit_id, $data);
			
			
			if($check){
    			$this->_redirect('/admin/payment-history?msg=' . 'データが更新されました。');
    		}else{
    			$this->_redirect('/admin/payment-history?msg=' . 'データが更新できません。');
    		} 
    	}
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/admin";
    	$nav['統計'] = "/admin/stactatics";
    	$nav['実績と課徴金'] = "/admin/payment-history";
    	$nav['実績と課徴金変更'] = "#";
    	$view->nav = $nav;
    }
}